//
//  XLHomeView.m
//  industry
//
//  Created by xl on 2020/11/12.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLHomeView.h"
#import "XLHomeViewProtocol.h"
#import "XLHomeAdapter.h"
#import "XLHomeCell.h"
#import "XLHomeTitleHeaderView.h"
#import "XLHomePresenterProtocol.h"
#import "XLHomeInteractorProtocol.h"
#import "XLAlert.h"
#import "XLAppletModel.h"
#import "XLPushTool.h"
#import "XLPopNoticeView.h"
#import "WXApi.h"
#import "YQShareSheetView.h"

@interface XLHomeView ()<XLHomeViewProtocol, XLCollectionAdapterDelegate>

@property (nonatomic, strong) XLCollectionView *collectionView;
@property (nonatomic, strong) XLHomeAdapter *adpter;

@property (nonatomic, strong) XLImageView *unOpenIG;
@property (nonatomic, strong) XLLabel *infoLB;
@property (nonatomic, strong) YQShareSheetView *sheetView;

//调试专用扫码
@property (nonatomic, strong) XLButton *scanBtn;

@end

@implementation XLHomeView

- (void)dealloc {
    XLLog(@"XLHomeViewProtocol dealloc");
}

#pragma mark - XLHomeViewProtocol
- (void)buildView {
    self.backgroundColor = [UIColor whiteBgColor];
    [self addSubview:self.collectionView];
    [self addSubview:self.unOpenIG];
    [self addSubview:self.infoLB];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self);
    }];
    
    [_unOpenIG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(NAVIGATIONBAR_HEIGHT + 98);
        make.centerX.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(265, 164));
    }];
    
    [_infoLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.unOpenIG.mas_bottom).offset(40);
        make.centerX.equalTo(self);
    }];
    
    if (XL_UserInfoData) {
        [self resetDefault];
    }

    //调试专用扫码，不用直接屏蔽这里代码就行了
//    [self addSubview:self.scanBtn];
}

- (void)refreshCollectionView:(NSArray *)allArr
                     indexArr:(NSArray *)indexArr
                      funcArr:(NSArray *)funcArr
                    commonArr:(NSArray *)commonArr
                     popupArr:(NSArray *)popupArr {
    _adpter.commonArr = commonArr;
    _adpter.allArr = allArr;
    _adpter.indexArr = indexArr;
    [_adpter.headerView refreshIndexDatas:indexArr];
    [_collectionView reloadData];
    
    // 弹窗显示通知
    [XLPopNoticeView showNoticeArray:popupArr];
}

- (void)refreshNewData {
    if (XL_UserInfoData) {
        //刷新用户数据
        [_adpter.headerView refreshUserInfo:XL_UserInfoData];
        if ([XLUserData sharedXLUserData].isHomeFirst) {
            [self resetDefault];
        }
        
        //绑定userID
        [[XLPushTool sharedXLPushTool] bindXLIdentifier];
    }
}

- (void)refreshCommonApplet {
    XLSend(self.baseController.presenter, XLHomePresenterProtocol, requestHomeAllInfo);

}

//刷新指标数据
- (void)refreshIndexsData:(NSArray *)indexArr {
    _adpter.indexArr = indexArr;
    [_adpter.headerView refreshIndexDatas:indexArr];
}


#pragma mark - XLCollectionAdapterDelegate
- (void)didSelectCellData:(id)cellData {
    XLSend(self.baseController.presenter, XLHomePresenterProtocol, handleCellData:cellData);
}

#pragma mark - UIButton
- (void)clickScan {
//    NSArray *iconsArr = [[NSBundle mainBundle] infoDictionary][@"CFBundleIcons"][@"CFBundlePrimaryIcon"][@"CFBundleIconFiles"];
//
//   UIImage * iamge =  [UIImage imageNamed:iconsArr.lastObject];

    

}

#pragma mark - Private
- (void)resetDefault {
    [XLUserData sharedXLUserData].isHomeFirst = NO;
    if (XL_UserInfoData.isStaff) {
        _unOpenIG.hidden = YES;
        _infoLB.hidden = YES;
        XLSend(self.baseController.presenter, XLHomePresenterProtocol, requestHomeAllInfo);
        if (ISEmptyString(XL_UserInfoData.lastLoginCompany)) {
            [self performSelector:@selector(delaySelectCompanyMethod) withObject:nil/*可传任意类型参数*/ afterDelay:1.0];
        }
    } else {
        _adpter.indexArr = nil;
        _adpter.commonArr = nil;
        _adpter.allArr = nil;
        [_collectionView reloadData];
        _unOpenIG.hidden = NO;
        _infoLB.hidden = NO;
        if (ISEmptyString(XL_UserInfoData.employeeName)) {
            [self performSelector:@selector(delaySelectUserMethod) withObject:nil/*可传任意类型参数*/ afterDelay:1.0];
        }
    }

}

- (void)delaySelectCompanyMethod {
    [XLAlert showAlertTitle:@"请先选择相关联公司" okTitle:@"立即前往" okHandler:^(UIAlertAction *action) {
         XLSend(self.baseController.interactor, XLHomeInteractorProtocol, gotoSwitchCompanyVC);
    }];
}

- (void)delaySelectUserMethod {
    [XLAlert showAlertTitle:@"请先绑定客户" okTitle:@"立即前往" okHandler:^(UIAlertAction *action) {
         XLSend(self.baseController.interactor, XLHomeInteractorProtocol, gotoSwitchUserVC);
    }];
}

#pragma mark - Setter/getter
- (XLCollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
        flow.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [[XLCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flow];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.delegate = self.adpter;
        _collectionView.dataSource = self.adpter;
        
        //注册cell
        [_collectionView registerClass:[XLHomeCell class] forCellWithReuseIdentifier:@"XLHomeCell"];
        [_collectionView registerClass:[XLHomeTitleHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"XLHomeTitleHeaderView"];
        [_collectionView registerClass:[XLHomeHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"XLHomeHeaderView"];
    }
    return _collectionView;
}

- (XLHomeAdapter *)adpter {
    if (!_adpter) {
        _adpter = [[XLHomeAdapter alloc] init];
        _adpter.adapterDelegate = self;
    }
    return _adpter;
}

- (XLButton *)scanBtn {
    if (!_scanBtn) {
        _scanBtn = [[XLButton alloc] initWithFrame:CGRectMake(self.width - 50, self.height - SAFEAREA_BOTTOM_HEIGHT - 100, 44, 44)];
        _scanBtn.backgroundColor = [UIColor redColor];
        _scanBtn.titleLabel.font = [UIFont fontOfPingFangSCRegularWithSize:14];
        [_scanBtn setTitle:@"扫一扫" forState:UIControlStateNormal];
        [_scanBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_scanBtn addTarget:self action:@selector(clickScan) forControlEvents:UIControlEventTouchUpInside];
    }
    return _scanBtn;
}

- (XLImageView *)unOpenIG {
    if (!_unOpenIG) {
        _unOpenIG = [XLImageView new];
        _unOpenIG.image = XLIMAGE(@"zwktbg");
        _unOpenIG.hidden = YES;
    }
    return _unOpenIG;
}

- (XLLabel *)infoLB {
    if (!_infoLB) {
        _infoLB = [XLLabel new];
        _infoLB.font = [UIFont fontOfPingFangSCRegularWithSize:14];
        _infoLB.textColor = [UIColor graySecondFontColor];
        _infoLB.text = @"暂未开通，敬请期待...";
        _infoLB.hidden = YES;
    }
    return _infoLB;
}

@end
